package com.tanhua.management.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.management.service.UserFreezeService;
import com.tanhua.management.service.UserService;
import com.tanhua.management.vo.PageResult;
import com.tanhua.management.vo.UserFreezeVo;
import com.tanhua.management.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Author: sun
 * DateTime: 2021/4/26
 **/

@RestController
@RequestMapping("/management/manage/users")
public class UserController {

    @Autowired
    private UserService usersService;

    @Autowired
    private UserFreezeService userFreezeService;


    //查询用户基本资料，数据分页;
    @GetMapping
    public PageResult queryUserByPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                      @RequestParam(value = "id", required = false) Long id,
                                      @RequestParam(value = "nickname", required = false) String nickname,
                                      @RequestParam(value = "city", required = false) String city) throws Exception {
        return this.usersService.queryUserByPage(page, pageSize, id, nickname, city);
    }


    //查询用户详细资料;
    @GetMapping("{userID}")
    public ResponseEntity<Object> queryUserById(@PathVariable(name = "userID") Long userId) {
        //调用usersService根据id查询用户数据;
        UserVo userVo = this.usersService.queryUserById(userId);
        try {
            //将json数据响应到前端;
            return ResponseEntity.ok(new ObjectMapper().writeValueAsString(userVo));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //响应失败状态;
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    //冻结用户;
    @PostMapping("freeze")
    public ResponseEntity<String> freeze(@RequestBody UserFreezeVo userFreezeVo) {
        Boolean bool = this.userFreezeService.freeze(userFreezeVo);
        //响应成功;
        if (bool) {
            return ResponseEntity.ok("已将该用户打入冷宫~");
        }
        //响应失败状态;
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    //解冻用户;
    @PostMapping("unfreeze")
    public ResponseEntity<String> unfreeze(@RequestBody UserFreezeVo userFreezeVo) {
        Boolean bool = this.userFreezeService.unfreeze(userFreezeVo);
        //响应成功;
        if (bool) {
            return ResponseEntity.ok("已重新宠幸该用户~");
        }
        //响应失败状态;
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
